#include <bits/stdc++.h>
#include <string>
using namespace std;

#define MAX 9999999

/**
 *
6 9
1 2 1
1 3 12
2 3 9
2 4 3
3 5 5
4 3 4
4 5 13
4 6 15
5 6 4
1 4
*
*/
int main()
{
    int n, m, b, e, l;
    cin >> n >> m;
    int graph[n + 1][n + 1];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            graph[i][j] = (i == j ? 0 : MAX);
        }
    }
    for (int i = 0; i < m; i++)
    {
        cin >> b >> e >> l;
        graph[b][e] = l;
    }
    cin >> b >> e;
    int vis[n + 1] = {0};
    int dis[n + 1] = {0};
    for (int i = 1; i <= n; i++)
    {
        dis[i] = graph[b][i];
    }
    for (int i = 1; i <= n; i++)
    {
        int small = MAX;
        for (int j = 1; j <= n; j++)
        {
            if (dis[j] < small && !vis[j])
            {
                small = dis[j];
                l = j;
            }
        }
        vis[l] = 1;
        for (int k = 1; k <= n; k++)
        {
            if (graph[l][k] != MAX)
            {
                dis[k] = min(dis[k], dis[l] + graph[l][k]);
            }
        }
    }
    cout << dis[e];
    return 0;
}